<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/13
 * Time: 10:29
 */

namespace App\Rep;


use App\Models\Material;
use Illuminate\Support\Facades\DB;

class MaterialRep
{

    /**
     * 获取材料列表
     * @param $params
     */
    public function getMaterialListRep($params){
        return Material::Search($params)
            ->with(['supplier','type','color','specification'])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??100);
    }

    /**
     * 添加材料
     * @param $params
     */
    public function createMaterialRep($params){
        if(isset($params['id']) && !empty($params['id'])){
            $model = Material::where('id',$params['id'])->first();
        }else{
            $model = new Material();
        }
        $model->name = $params['name'];
        $model->code = $params['code'];
        $model->type_id = $params['type_id'];
        $model->model_number = $params['model_number']??'';
        $model->specification_id = $params['specification_id']??'';
        $model->color_id = $params['color_id'];
        $model->unit = $params['unit'];
        $model->supplier_id = $params['supplier_id'];//供应商
        $model->w_price = $params['w_price'];
        $model->d_price = $params['d_price'];
        $model->initial_inventory = $params['initial_inventory'];
        $model->initial_price = $params['initial_price'];
        $model->initial_min = $params['initial_min'];
        $model->initial_max = $params['initial_max'];
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }

    /**
     * 删除材料
     * @param $params
     */
    public function delMaterialRep($params){
        $model = Material::where('id',$params['id'])->first();
        $model->is_del = $params['is_del'];
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }
}